package cn.codergege.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@ComponentScan
@EnableJpaRepositories(basePackages = "cn.codergege.demo.dao.primary",
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "txmPrimary")
@EnableTransactionManagement
public class JpaPrimaryConfig {

    @Autowired
    private Environment env;

    @Autowired @Qualifier("primaryDs")
    private DataSource dataSource;

    private String getEnvProperty(String propertyName) {
        return env.getProperty(propertyName);
    }

//    private int getIntEnvProperty(String propertyName) {
//        return Integer.parseInt(env.getProperty(propertyName));
//    }

    @Configuration
    @Profile({"development", "test"})
    @PropertySource("classpath:development/db.properties")
    static class Development {
    }

    @Configuration
    @Profile("production")
    @PropertySource("classpath:production/db.properties")
    static class Production {
}
    // 异常处理
    /*
    @Bean
    public BeanPostProcessor persistenceTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
    */

    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setPersistenceUnitName("persistenceUnitPrimary");
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactory.setPackagesToScan("cn.codergege.demo.domain.primary");

        // 设置 jpaProperties
        Properties jpaProperties = new Properties();
        jpaProperties.put(org.hibernate.cfg.Environment.DIALECT,
                getEnvProperty("jpa.dialect"));
        jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL,
                getEnvProperty("jpa.show_sql"));
        jpaProperties.put(org.hibernate.cfg.Environment.FORMAT_SQL,
                getEnvProperty("jpa.format_sql"));
        jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO,
                env.getProperty("jpa.hbm2ddl.auto"));

        entityManagerFactory.setJpaProperties(jpaProperties);

        return entityManagerFactory;
    }

    @Bean(name = "txmPrimary")
    public PlatformTransactionManager transactionManager(EntityManagerFactory  entityManagerFactoryPrimary) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryPrimary);
        return jpaTransactionManager;
    }

    // 一般就用不到 JdbcTemplate 了
    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }

}
			